/* @author cerimice */
package Dialogos.Sica;

import Logica.Dialogo.Sica.LogicaLogin;

import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import com.vaadin.ui.MenuBar;
import com.vaadin.Application;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.terminal.ThemeResource;

public class Login extends Application
    {
    private LogicaLogin     logicaLogin;
    
    private Window mainWindow;
    
    private Label           etiqueta;
    private TextField       usuario;
    private PasswordField   password;
    private Button          accesar;
    
    public  LogicaLogin getLoginData(){return logicaLogin;}

    @Override
    public void init()
        {iniciarVentana("SICA - Sistema Informático de Control Administrativo");}
    
    private void iniciarVentana(String tituloVentana)
        {
        setTheme("sica");
        mainWindow = new Window(tituloVentana);
        setMainWindow(mainWindow);
        agregarComponentes();
        }
    
    private void agregarComponentes()
        {
        logicaLogin = new LogicaLogin();
                
        etiqueta    = new Label();
        usuario     = new TextField("Usuario");
        password    = new PasswordField("Password");
        accesar     = new Button("Accesar");
        
        etiqueta.setIcon(new ThemeResource("images/logo.png"));
        
        MenuBar menubar = new MenuBar();
            menubar.setIcon(new ThemeResource("images/logo.png"));
            menubar.setHeight("20px");
            menubar.setWidth("100%");
        
        VerticalLayout componentes = new VerticalLayout();
            componentes.addComponent(menubar);  componentes.setComponentAlignment(menubar,Alignment.TOP_CENTER);
            componentes.addComponent(usuario);  componentes.setComponentAlignment(usuario,Alignment.TOP_CENTER);
            componentes.addComponent(password); componentes.setComponentAlignment(password,Alignment.TOP_CENTER);
            componentes.addComponent(accesar);  componentes.setComponentAlignment(accesar,Alignment.TOP_CENTER);
        
        accesar.addListener(new Button.ClickListener()  {@Override public void buttonClick(Button.ClickEvent event){validarAcceso();}});

        mainWindow.setContent(componentes);
        //validarAcceso2();
        }
        
    private void validarAcceso()
        {
        try
            {
            String user = (String)usuario.getValue();
            String pass = (String)password.getValue();
            if(logicaLogin.comprobarAcceso(user,pass))
                {
                mainWindow.removeAllComponents();
                mainWindow.setContent(new Principal(mainWindow,logicaLogin.getUsuario()).getMenu());
                }
            else
                {mainWindow.showNotification("Error",logicaLogin.getError(),Window.Notification.TYPE_WARNING_MESSAGE);}
            }
        catch (Exception ex)
            {mainWindow.showNotification("Error",ex.getMessage());}
        }
    
    private void validarAcceso2()
        {
        try
            {
            String user = "cerimice";
            String pass = "cerimice";
            if(logicaLogin.comprobarAcceso(user,pass))
                {
                mainWindow.removeAllComponents();
                mainWindow.setContent(new Principal(mainWindow,logicaLogin.getUsuario()).getMenu());
                }
            else
                {mainWindow.showNotification("Error",logicaLogin.getError(),Window.Notification.TYPE_WARNING_MESSAGE);}
            }
        catch (Exception ex)
            {mainWindow.showNotification("Error",ex.getMessage());}
        }
    }
